#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N = 2e5 + 10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//

void solve()
{
    string s1;
    cin >> s1;
    int sz = 0;
    for(auto &k : s1)
    {
        if(k == '0') sz++;
    }

    if(s1.size() == 1 || (sz == (int)(s1.size() - sz) && (int)(s1.size()) == 2)) cout << 0 << '\n';
    else if(sz != (int)(s1.size() - sz)) cout << min(sz, (int)(s1.size() - sz)) << '\n';
    else cout << sz - 1 << '\n';



}

int main()
{
    close();
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}